
import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class MyTest {
    @Test
    public void druidSpring() throws SQLException {
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
        DruidDataSource druid= (DruidDataSource) applicationContext.getBean("druid");
        System.out.println(druid.getConnection());
        applicationContext.close();
    }
    @Test
    public void c3p0Spring() throws SQLException {
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
        ComboPooledDataSource c3p0= (ComboPooledDataSource) applicationContext.getBean("c3p0");
        System.out.println(c3p0.getConnection());
        applicationContext.close();
    }
    @Test
    public void druidTest() throws Exception {
        ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");

        //使用无参构造方法创建数据源对象
        DruidDataSource druid=new DruidDataSource();
        //通过setter方法对连接属性进行赋值
        druid.setDriverClassName(jdbc.getString("jdbc.driver"));
        druid.setUrl(jdbc.getString("jdbc.url"));
        druid.setUsername(jdbc.getString("jdbc.username"));
        druid.setPassword(jdbc.getString("jdbc.password"));
        //获取连接
        Connection connection = druid.getConnection();
        System.out.println(connection);
        //归还连接 关闭连接池
        connection.close();
        druid.close();
    }
    @Test
    public void c3p0Test() throws Exception {
        //实际开发时仅配置一个数据源即可 但是我们一般不会直接在代码中写死配置
        //而是一般将 配置抽离出来创建jdbc.properties
        //所以数据源应该读取jdbc.properties进行配置
        //ResourceBundle就是简单读取properties的工具类
        //无需书写文件全名 自动加载类路径下指定名称的properties文件
        //通过getString(name)方法进行使用
        ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");

        //使用无参构造方法创建数据源对象
        ComboPooledDataSource c3p0=new ComboPooledDataSource();
        //通过setter方法对连接属性进行赋值
        c3p0.setDriverClass(jdbc.getString("jdbc.driver"));
        c3p0.setJdbcUrl(jdbc.getString("jdbc.url"));
        c3p0.setUser(jdbc.getString("jdbc.username"));
        c3p0.setPassword(jdbc.getString("jdbc.password"));
        //获取连接
        Connection connection = c3p0.getConnection();
        System.out.println(connection);
        //归还连接 关闭连接池
        connection.close();
        c3p0.close();
    }
    @Test
    public void druid() throws Exception {
        //使用无参构造方法创建数据源对象
        DruidDataSource druid=new DruidDataSource();
        //通过setter方法对连接属性进行赋值
        druid.setDriverClassName("com.mysql.jdbc.Driver");
        druid.setUrl("jdbc:mysql:///test");
        druid.setUsername("root");
        druid.setPassword("123456");
        //获取连接
        Connection connection = druid.getConnection();
        System.out.println(connection);
        //归还连接 关闭连接池
        connection.close();
        druid.close();
    }
    @Test
    public void c3p0() throws Exception {
        //使用无参构造方法创建数据源对象
        ComboPooledDataSource c3p0=new ComboPooledDataSource();
        //通过setter方法对连接属性进行赋值
        c3p0.setDriverClass("com.mysql.jdbc.Driver");
        c3p0.setJdbcUrl("jdbc:mysql:///test");
        c3p0.setUser("root");
        c3p0.setPassword("123456");
        //获取连接
        Connection connection = c3p0.getConnection();
        System.out.println(connection);
        //归还连接 关闭连接池
        connection.close();
        c3p0.close();
    }
    @Test
    public void u1(){
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
        Object u1 = applicationContext.getBean("u1");
        System.out.println(u1);
        applicationContext.close();
    }
}
